package com.chilli.cosima.action.customermanager;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.struts2.ServletActionContext;

import com.chilli.cosima.action.BaseSupport;
import com.chilli.cosima.common.BussinessException;
import com.chilli.cosima.data.bean.Customer;
import com.chilli.cosima.service.CustomerService;
import com.chilli.cosima.utils.Area;
import com.chilli.cosima.utils.Country;
import com.chilli.cosima.utils.WorldCountryUtils;

public class CustomerAddAction extends BaseSupport
{
	private List<Area> areas;
	private Map<String, List<Country>> countrys;
	private List<String> languages = new ArrayList<String>();
	private Customer customer;
	private Long id;
	private File myFile;
	private String myFileFileName;
	private CustomerService customerService;

	@SuppressWarnings("static-access")
	public String jumpPage()
	{
		areas = WorldCountryUtils.getWorldCountryUtils().areas;
		countrys = WorldCountryUtils.getWorldCountryUtils().countrys;
		languages.add("中文(Chinese)");
		languages.add("英语(English)");
		languages.add("西班牙语(Spanish)");
		languages.add("德语(German)");
		languages.add("法语(French)");
		return JUMP_PAGE;
	}

	@Override
	public String execute()
	{
		try
		{
			if (null != myFile)
			{
				String imageFileName = new Date().getTime()
						+ getExtention(myFileFileName);

				File imageFile = new File(ServletActionContext
						.getServletContext().getRealPath(
								"/upload/images/customer")
						+ "/" + imageFileName);

				copy(myFile, imageFile);

				customer.setPicture(imageFileName);
			}

			if (null != customer.getMobilePhone()
					&& !"".equals(customer.getMobilePhone()))
			{
				customer.setVipCardNumber(customer.getMobilePhone());
			}

			customerService.createCustomer(customer);
			return SUCCESS;
		} catch (BussinessException e)
		{
			logger.error("error", e);
		} catch (Exception e)
		{

		}
		return JUMP_PAGE;
	}

	private static final long serialVersionUID = 2966991297994658226L;

	public List<Area> getAreas()
	{
		return areas;
	}

	public void setAreas(List<Area> areas)
	{
		this.areas = areas;
	}

	public Map<String, List<Country>> getCountrys()
	{
		return countrys;
	}

	public void setCountrys(Map<String, List<Country>> countrys)
	{
		this.countrys = countrys;
	}

	public CustomerService getCustomerService()
	{
		return customerService;
	}

	public void setCustomerService(CustomerService customerService)
	{
		this.customerService = customerService;
	}

	public Customer getCustomer()
	{
		return customer;
	}

	public void setCustomer(Customer customer)
	{
		this.customer = customer;
	}

	public Long getId()
	{
		return id;
	}

	public void setId(Long id)
	{
		this.id = id;
	}

	public File getMyFile()
	{
		return myFile;
	}

	public void setMyFile(File myFile)
	{
		this.myFile = myFile;
	}

	public String getMyFileFileName()
	{
		return myFileFileName;
	}

	public void setMyFileFileName(String myFileFileName)
	{
		this.myFileFileName = myFileFileName;
	}

	public List getLanguages()
	{
		return languages;
	}

	public void setLanguages(List languages)
	{
		this.languages = languages;
	}

}
